package com.shaozi.nc.web.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shaozi.lib.core.ActionRetBean;
import com.shaozi.nc.io.mq.OrgGroupProducer;
import com.shaozi.nc.web.action.base.BaseActionImpl;
import com.shaozi.nc.web.action.bean.OrgChatGroupBean;
import com.shaozi.nc.web.beans.DeptGroupBean;
import com.shaozi.nc.web.beans.GenChatGroupsBean;
import com.shaozi.nc.web.beans.GetChatGroupMemberReqBean;
import com.shaozi.nc.web.biz.ChatGroupBiz;
import com.shaozi.utils.common.BeanUtils;

@Controller
@RequestMapping("/chatGroup")
public class ChatGroupAction extends BaseActionImpl {

	@Autowired
	private ChatGroupBiz bizChatGroup;

	public ChatGroupAction() {

	}

	@RequestMapping(value = "/genOrgChatGroup", method = RequestMethod.POST, consumes = "application/json;charset=UTF-8")
	@ResponseBody
	public ActionRetBean genOrgChatGroup(HttpServletRequest req, HttpServletResponse res,
			@RequestBody GenChatGroupsBean reqBean) {
		ActionRetBean retBean = new ActionRetBean();
		if (!validGenChatGroupBean(retBean, reqBean)) {
			return retBean;
		}

		OrgGroupProducer producer = (OrgGroupProducer) BeanUtils.getBean("orgGroupProducer");
		for (DeptGroupBean deptGroup : reqBean.getDeptGroups()) {
			OrgChatGroupBean orgChatGroupBean = new OrgChatGroupBean();
			orgChatGroupBean.setUidList(deptGroup.getUserIdList());
			orgChatGroupBean.setDeptName(deptGroup.getDeptName());
			orgChatGroupBean.setCompanyId(reqBean.getCompanyId());
			orgChatGroupBean.setDeptId(deptGroup.getDeptId());
			orgChatGroupBean.setOperate(deptGroup.getOperate());
			producer.pushMsg(orgChatGroupBean);
		}
		return retBean;
	}

	@RequestMapping(value = "/getMembers", method = RequestMethod.POST, consumes = "application/json")
	@ResponseBody
	public ActionRetBean getChatGroupMembers(HttpServletRequest req, HttpServletResponse res,
			@RequestBody GetChatGroupMemberReqBean reqBean) {
		ActionRetBean retBean = new ActionRetBean();
		if (!validGetChatGroupMembers(retBean, reqBean)) {
			return retBean;
		}
		bizChatGroup.getChatGroupMembers(retBean, reqBean);
		return retBean;
	}

	@RequestMapping(method = RequestMethod.POST, consumes = "application/json")
	@ResponseBody
	public ActionRetBean get(HttpServletRequest req, HttpServletResponse res,
			@RequestBody(required = false) String companyId) {
		return new ActionRetBean();
	}

	private boolean validGenChatGroupBean(ActionRetBean retBean, GenChatGroupsBean reqBean) {
		if (StringUtils.isEmpty(reqBean.getCompanyId())) {
			retBean.setCode(-1);
			retBean.setMsg("company id can not be null");
			return false;
		}
		if (reqBean.getDeptGroups() == null || reqBean.getDeptGroups().size() == 0) {
			retBean.setCode(-1);
			retBean.setMsg("groups can not be null");
		}
		for (DeptGroupBean deptGroup : reqBean.getDeptGroups()) {
			if (StringUtils.isEmpty(deptGroup.getDeptName()) || deptGroup.getUserIdList() == null
					|| deptGroup.getUserIdList().size() == 0) {
				retBean.setCode(-1);
				retBean.setMsg("groups name and user id list can not be null");
			}
		}
		return true;
	}

	private boolean validGetChatGroupMembers(ActionRetBean retBean, GetChatGroupMemberReqBean reqBean) {
		if (StringUtils.isEmpty(reqBean.getCompany_id())) {
			retBean.setCode(-1);
			retBean.setMsg("company id is empty");
			return false;
		}
		if (StringUtils.isEmpty(reqBean.getGroup_id())) {
			retBean.setCode(-1);
			retBean.setMsg("group id is empty");
			return false;
		}
		return true;
	}
}
